package com.sdc.autohome.service.impl;

import com.opencsv.CSVWriter;
import com.sdc.autohome.bean.ClueBack;
import com.sdc.autohome.mapper.ClueBackServiceMapper;
import com.sdc.autohome.service.ClueBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName:ClueBackServiceImpl
 * @Auther: Liu
 * @Date: 2024/11/28 22:21
 */
@Service
public class ClueBackServiceImpl implements ClueBackService {
    @Autowired
    private ClueBackServiceMapper clueBackServiceMapper;

    @Value("${api.csvFilePath}")
    private String csvFilePath;

    @Override
    public List<ClueBack> findByBs(String date) throws IOException {

        List<ClueBack> byBs = clueBackServiceMapper.findByBs(date);
        try(CSVWriter csvWriter = new CSVWriter(new FileWriter(csvFilePath))){
            csvWriter.writeNext(new String[]{"clue_phone","type","time","ds"});

            // 写入每一条记录
            for (ClueBack byB : byBs) {
                csvWriter.writeNext(new String[]{
                        byB.getCluePhone(),
                        byB.getType(),
                        byB.getTime(),
                        byB.getDs()
                });
            }
        }

        return byBs;

    }
}
